/**
 * Created by xeesh on 11/15/2015.
 */
$(document).ready(function(){
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    var Modal = $(".modal-msg");
    Modal.fadeOut(0);
    $('form').submit(function(){
        preventDefault();
    });
});




/*
    Basic Operations for container
 */
function createContainer(){
    $('#createContainer').hide();
    var containerName = $(".container-name").val();
    $.ajax({
        method:'POST',
        url: url+"/container/create",
        data: {'container':containerName},
        success: function(response){
            var res = JSON.parse( response );
            var Modal = $(".modal-msg");
            if(res.statusCode == 200){
                Modal.addClass("msg-success")
                Modal.addClass("text-success")
                Modal.html(res.container+": Created Succesfully" ).slideDown(500);
                Modal.delay(3000);
                location.reload();
                Modal.slideUp(500);
            }else if(res.statusCode==400){
                Modal.addClass("msg-error")
                Modal.addClass("text-danger")
                Modal.html(res.errorMessage ).slideDown(500);
                Modal.delay(4000);
                Modal.slideUp(500);
            }
        },
    });
}

function deleteContainerModal(id){
    var name = $(".row-"+id).find('.name').text();
    var containerNameSpan = $('.item-name');
    containerNameSpan.html(name);
    $("#deleteContainer").modal();
}

function deleteContainerProcess(){
    var containerName = $('.item-name').text();
    $.ajax({
        method:'POST',
        url: url+"/container/delete",
        data: {'container':containerName},
        success: function(response){
            var res = JSON.parse( response );
            var Modal = $(".modal-msg");
            if(res.statusCode == 200){
                Modal.addClass("msg-success");
                Modal.addClass("text-success");
                Modal.removeClass("text-danger");
                Modal.html(res.container+": Deleted Succesfully" ).slideDown(500);
                Modal.delay(3000);
                location.reload();
                Modal.slideUp(500);
            }else if(res.statusCode==400){
                Modal.addClass("msg-error")
                Modal.addClass("text-danger")
                Modal.removeClass("text-success")
                Modal.html(res.errorMessage ).slideDown(500);
                Modal.delay(4000);
                Modal.slideUp(500);
            }
        },
    });
}


/*
    Basic Operations for Blobs
 */

function uploadBlob(){
    $('#uploadBlobModal').hide();
    var blobName = $(".blob-name").val();
    var containerName = $(".container-name").val();
    var file = $(".blob-file").val();
    $.ajax({
        method:'POST',
        url: url+"/blob/upload",
        data: {
            'name':blobName,
            'file': file,
            'container':containerName
        },
        success: function(response){
            var res = JSON.parse( response );
            var Modal = $(".modal-msg");
            if(res.statusCode == 200){
                Modal.addClass("msg-success")
                Modal.addClass("text-success")
                Modal.html(res.container+": Blob Uploaded" ).slideDown(500);
                Modal.delay(3000);
                location.reload();
                Modal.slideUp(500);
            }else if(res.statusCode==400){
                Modal.addClass("msg-error")
                Modal.addClass("text-danger")
                Modal.html(res.errorMessage ).slideDown(500);
                Modal.delay(4000);
                Modal.slideUp(500);
            }
        },
    });
}

/**
 * This function sets the name in the Delete Blob Modal
 * @param id: It is the id of the row where the click action is performed
 */
function deleteBlobModal(id){
    var name = $(".row-"+id).find('.name').text();
    var containerNameSpan = $('.item-name');
    containerNameSpan.html(name);
    $("#deleteBlobModal").modal();
}

function deleteBlobProcess(){
    var containerName = $(".container-name").val();
    var blobName =  $(".item-name").text();
    $.ajax({
        method:'POST',
        url: url+"/blob/delete",
        data: {'container':containerName,'blob':blobName},
        success: function(response){
            var res = JSON.parse( response );
            var Modal = $(".modal-msg");
            if(res.statusCode == 200){
                Modal.addClass("msg-success");
                Modal.addClass("text-success");
                Modal.removeClass("text-danger");
                Modal.html(res.blob+": Deleted Succesfully" ).slideDown(500);
                Modal.delay(3000);
                location.reload();
                Modal.slideUp(500);
            }else if(res.statusCode==400){
                Modal.addClass("msg-error")
                Modal.addClass("text-danger")
                Modal.removeClass("text-success")
                Modal.html(res.errorMessage ).slideDown(500);
                Modal.delay(4000);
                Modal.slideUp(500);
            }
        },
    });
}